import requests

# 附件的URL
attachment_url = 'http://example.com/path/to/attachment.pdf'

# 发送GET请求到附件的URL
response = requests.get(attachment_url, stream=True)

# 检查请求是否成功
if response.status_code == 200:
    # 设置本地保存的文件名
    local_filename = 'downloaded_attachment.pdf'

    # 以二进制模式打开文件以写入
    with open(local_filename, 'wb') as f:
        # 迭代响应内容并写入文件
        for chunk in response.iter_content(chunk_size=8192):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)
                f.flush()  # 确保数据被写入磁盘
    print(f"Attachment {local_filename} has been downloaded successfully.")
else:
    print(f"Failed to retrieve the attachment. Status code: {response.status_code}")